package com.sphd.beautysalon.model;

import com.android.volley.VolleyError;
import com.sphd.beautysalon.info.SalesInfo;
import com.sphd.beautysalon.interfaces.VolleyJsonObjectCallBack;
import com.sphd.beautysalon.presenter.SalesPresenterImpl;
import com.wzh.frame.utils.JsonUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by Hu on 2016/11/7.
 */

public class SalesModelImpl implements SalesModel, VolleyJsonObjectCallBack {
    SalesPresenterImpl salesPresenter;

    @Override
    public void sendUrl(String url, SalesPresenterImpl salesPresenter) {
        this.salesPresenter = salesPresenter;
        volleyUtil.setJsonObjectRequest(url, this);
    }

    @Override
    public void onResponse(JSONObject response) {
        double topup;
        double inMoeny;
        double outMoney;
        double enterMoney;
        int listnumber;
        try {
            topup = response.getDouble("topup");
            inMoeny = response.getDouble("listsum0");
            outMoney = response.getDouble("listsum1");
            enterMoney = response.getDouble("listsum2");
            listnumber = response.getInt("listnumber");
            List<SalesInfo> salesInfos = JsonUtil.getListFromString(response.getString("list"), SalesInfo.class);
            salesPresenter.sendRes(topup, inMoeny, outMoney, enterMoney, listnumber, salesInfos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onErrorResponse(VolleyError error) {
        salesPresenter.sendError(error);
    }
}
